<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 编辑模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisiblebj" width="30%">
      <el-form
        :model="ruleFormbj"
        :rules="rulesbj"
        ref="ruleFormbj"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleFormbj.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleFormbj.email"></el-input>
        </el-form-item>
        <el-form-item label="电弧" prop="mobile">
          <el-input v-model="ruleFormbj.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblebj = false">取 消</el-button>
        <el-button type="primary" @click="submitFormbj('ruleFormbj')">确 定</el-button>
      </span>
    </el-dialog>
    <!--  -->

    <!-- 添加对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
    <!--  -->
    <el-card style="margin-top: 10px">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="keyword" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="getdata()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="14">
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </el-col>
      </el-row>

      <el-table :data="users" border style="width: 100%">
        <el-table-column type="index" label="#" width="180"> </el-table-column>
        <el-table-column prop="username" label="姓名" width="180"> </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"> </el-table-column>
        <el-table-column prop="mobile" label="电话" width="180"> </el-table-column>
        <el-table-column prop="role_name" label="角色" width="180"> </el-table-column>
        <el-table-column prop="mg_state" label="状态" width="180">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state" @change="changestate(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="edit(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="del(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage2"
        :page-sizes="[2, 3, 4, 5]"
        :page-size="pagesize"
        layout="sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      users: [],
      keyword: "",
      pagenum: 1,
      pagesize: 5,
      total: 0,
      currentPage2: 1,
      ruleForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },

      dialogVisiblebj: false,
      ruleFormbj: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      rulesbj: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    edit(row) {
      this.dialogVisiblebj = true;
      this.ruleFormbj = row;
    },
    submitFormbj(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.put("users/" + this.ruleFormbj.id, this.ruleFormbj).then((res) => {
            if (res.meta.status == 200) {
              this.$message.success("编辑成功");
              this.getdata();
              this.dialogVisiblebj=false
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    del(id) {
      this.$axios.delete("users/" + id).then((res) => {
        console.log(res, 555);
        if (res.meta.status == 200) {
          this.$message.success("删除成功");
          this.getdata();
        }
      });
    },
    changestate(row) {
      this.$axios.put(`users/${row.id}/state/${row.mg_state}`).then((res) => {
        console.log(res, 444);
        if (res.meta.status == 200) {
          this.$message.success("设置成功");
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagenum = val;
      this.getdata();
    },
    add() {
      var obj = {
        username: this.ruleForm.username,
        password: this.ruleForm.password,
        email: this.ruleForm.email,
        mobile: this.ruleForm.mobile,
      };
      this.$axios.post("users", obj).then((res) => {
        console.log(res, 333);
        if (res.meta.status == 201) {
          this.$message.success("创建成功");
          this.getdata();
          this.dialogVisible = false;
          this.ruleForm.username = "";
          this.ruleForm.password = "";
          this.ruleForm.email = "";
          this.ruleForm.mobile = "";
        }
      });
    },
    getdata() {
      this.$axios
        .get("users", {
          params: {
            query: this.keyword,
            pagenum: this.pagenum,
            pagesize: this.pagesize,
          },
        })
        .then((res) => {
          console.log(res, 222);
          this.users = res.data.users;
          this.total = res.data.total;
        });
    },
  },
};
</script>

<style lang="less" scoped></style>
